#!/bin/sh
#
# Copyright 2013 Open Source Robotics Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

[ -L ${0} ] && SCRIPT_DIR=$(readlink ${0}) || SCRIPT_DIR=${0}
SCRIPT_DIR=$(dirname ${SCRIPT_DIR})

. ${SCRIPT_DIR}/common

final="$@"

EXT=so
if [ $(uname) = "Darwin" ]; then
    EXT=dylib
fi

# add ros path plugin if it does not already exist in the passed in arguments
if [ `expr "$final" : ".*libgazebo_ros_paths_plugin\.$EXT.*"` -eq 0 ]
then
    final="$final -s `catkin_find --first-only libgazebo_ros_paths_plugin.$EXT`"
fi

# add ros api plugin if it does not already exist in the passed in arguments
if [ `expr "$final" : ".*libgazebo_ros_api_plugin\.$EXT.*"` -eq 0 ]
then
    final="$final -s `catkin_find --first-only libgazebo_ros_api_plugin.$EXT`"
fi

setup_path=$(pkg-config --variable=prefix gazebo)/share/gazebo/

# source setup.sh, but keep local modifications to GAZEBO_MASTER_URI
desired_master_uri="$GAZEBO_MASTER_URI"
. $setup_path/setup.sh
if [ "$desired_master_uri" = "" ]; then
        desired_master_uri="$GAZEBO_MASTER_URI"
fi

final=$(relocate_remappings "${final}")

GAZEBO_MASTER_URI="$desired_master_uri" gzserver $final
